//
//  LXNewFeatureViewController.m
//  绿稀全球购
//
//  Created by 诸葛孔明 on 16/7/16.
//  Copyright © 2016年 SJT. All rights reserved.
//
//********************************引导页********************************
#import "LXNewFeatureViewController.h"

#import "LXNavigationController.h"

#import "HomeViewController.h"

#import "LXNewFeatureCell.h"

#import "LCTabBarController.h"


#define LXGuidePageCount 3

@interface LXNewFeatureViewController ()

@property(nonatomic,weak)UIImageView *guideView;

@property(nonatomic,assign)CGFloat offsetX;

@property(nonatomic,weak)UIPageControl *pageControl;

@property(nonatomic,weak)UIButton *startButton;

@end

@implementation LXNewFeatureViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [self initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[LXNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    
//    [self setUpPageControl];
    [self setUpStartButton];
}

- (void)setUpPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = LXGuidePageCount;
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor HexColor:@"#81c034"];
    pageControl.centerX = self.view.width * 0.5;
    pageControl.y = self.view.height * 0.95;
    self.pageControl = pageControl;
    [self.view addSubview:pageControl];
}

- (void)setUpStartButton
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"跳过" forState:UIControlStateNormal];
    
    [btn setBackgroundColor:[UIColor HexColor:@"#81c034"]];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    btn.frame = CGRectMake(self.view.width -60-10, 20, 60, 25);
    btn.layer.cornerRadius = 10;
    
    [btn addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    self.startButton = btn;
}

- (void)startButtonClick
{
    LCTabBarController * tbvc = [[LCTabBarController alloc] init];
    [[UIApplication sharedApplication].delegate window].rootViewController= tbvc;
    [[[UIApplication sharedApplication].delegate window] makeKeyAndVisible];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return LXGuidePageCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LXNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.image = [UIImage imageNamed:[NSString stringWithFormat:@"launch_%zd",indexPath.row + 1]];
    
    if(indexPath.item==2){
        cell.islastPage = YES;
    }else{
        cell.islastPage = NO;
    }
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat offsetGap = offsetX - _offsetX;
    
    _guideView.x += 2 * offsetGap;
    
    [UIView animateWithDuration:0.25 animations:^{
        _guideView.x -= offsetGap;
    }];
    
    int page = offsetX / self.view.width + 1;
    _guideView.image = [UIImage imageNamed:[NSString stringWithFormat:@"launch%d",page]];
    _offsetX = offsetX;
    
    self.pageControl.currentPage = page - 1;
    
}

@end
